/**
 * Captain
 * © 2018 Captain copyright，All rights reserved.
 * http://www.sccaptain.com.cn
 * 
 * JAVA : 8
 * 文  件  名: OrderFormServiceImpl.java
 * 创  建  人: QinYoubin (qin.youbin@sccaptain.com.cn)
 * 创建时间: 2018年5月2日 下午2:02:11
 * 版         本: 1.0.0
 * 备         注:
 * 修订历史:
 */
package com.material.c2c.shoping.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.cpt.framework.core.OperationResult;
import com.material.c2c.commodity.dao.CommInfoDao;
import com.material.c2c.commodity.entity.SPXX;
import com.material.c2c.config.OrderFormStatus;
import com.material.c2c.config.PayMethods;
import com.material.c2c.config.ShoppingCartStatus;
import com.material.c2c.core.UUIDGenerator;
import com.material.c2c.shoping.dao.OrderFormDao;
import com.material.c2c.shoping.dao.OrderFormMapDao;
import com.material.c2c.shoping.dao.ShoppingCartDao;
import com.material.c2c.shoping.entity.DD_SP_MAP;
import com.material.c2c.shoping.entity.GWDD;

/**
 * 购物订单
 * @since 1.0
 * @version 1.0
 * @author QinYoubin (qin.youbin@sccaptain.com.cn)
 */
@Service
public class OrderFormServiceImpl implements OrderFormService{
  @Resource 
  private OrderFormDao orderFormDao;
  @Resource
  private OrderFormMapDao orderFormMapDao;
  @Resource
  private ShoppingCartDao cartDao;
  @Resource
  private CommInfoDao commInfoDao; 
  @Override
  @Transactional
  public GWDD add(GWDD gwdd,List<DD_SP_MAP> mapList,List<String> cartList) {
    String ddId = UUIDGenerator.generateUUIDStr();
    gwdd.setId(ddId);
    gwdd.setDdh(this.createOrderBh());
    gwdd.setZffs(PayMethods.NO_PAY);
    gwdd.setXdsj(new Date());
    gwdd.setDdzt(OrderFormStatus.NEW_CREATE);
    gwdd = orderFormDao.add(gwdd);
    if(gwdd == null) {
      return null;
    }
    for(DD_SP_MAP map : mapList) {
      map.setId(UUIDGenerator.generateUUIDStr());
      map.setDdid(ddId);
      orderFormMapDao.add(map);
      SPXX spxx = commInfoDao.getOne(map.getSpid());
      spxx.setLjpjsl(spxx.getLjpjsl()+map.getSpsl());
      commInfoDao.update(spxx);
    }
    // 更改购物车状态为已购买
    for(String cartId:cartList) {
    	cartDao.changeStatus(cartId, ShoppingCartStatus.BUY);
    }
    return gwdd;
  }
  
  @Override
  public List<GWDD> list(String id,int ddzt) {
    // 查询用户订单信息
    return orderFormDao.findByYhidAndDdztOrderByXdsjDesc(id,ddzt);
  }
  @Override
  public GWDD getOne(String id) {
	  return orderFormDao.getOne(id);
  }
  @Override
  @Transactional
  public OperationResult cancel(String id) {
    //取消订单，只更改状态
    orderFormDao.changeStatus(OrderFormStatus.CANCEL, id);
    return OperationResult.SUCCEED;
  }
  
  @Override
  @Transactional
  public OperationResult delete(String id) {
    //删除订单
    orderFormDao.delete(id);
    orderFormMapDao.deleteByDdid(id);
    return OperationResult.SUCCEED;
  }
  
  @Override
  @Transactional
  public OperationResult paid(String id, String payMethods) {
    //更改状态为已支付
	 GWDD dd = orderFormDao.getOne(id);
	 dd.setDdzt(OrderFormStatus.PAID);
	 dd.setZffs(payMethods);
    orderFormDao.update(dd);
    return OperationResult.SUCCEED;
  }
  @Override
  public Page<GWDD> getPage(int zt,Pageable pageable){
	  return orderFormDao.findAll(pageable);
  }
  @Override
  @Transactional
  public OperationResult dispatching(String id) {
    // 更改状态为配送中
    orderFormDao.changeStatus(OrderFormStatus.DISPATCHING, id);
    return OperationResult.SUCCEED;
  }
  
  @Override
  @Transactional
  public OperationResult dispatched(String id) {
    // 更改状态为配成功
    orderFormDao.changeStatus(OrderFormStatus.DISPATCHED, id);
    return OperationResult.SUCCEED;
  }
  @Override
  @Transactional
  public OperationResult finish(String id) {
    // 更改状态为配送中
    orderFormDao.changeStatus(OrderFormStatus.FINISH, id);
    return OperationResult.SUCCEED;
  }
  @Override
  @Transactional
  public OperationResult changeStatus(String id, int zt) {
	  orderFormDao.changeStatus(zt, id);
	  return OperationResult.SUCCEED;
  }
  private String createOrderBh() {
	  String bh="od";
	  Date now =new Date();
	  bh += now.toString();
	  int abc = (int)(Math.random()*1000);
	  bh += String.valueOf(abc);
	  return bh;
  }
}
